package com.example.rabbitmq.controller.delay;

import com.example.rabbitmq.Constants;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

//@RestController
//@RequestMapping("/producer")
public class DelayProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/delay")
    public String delay() {
        CorrelationData correlationData = new CorrelationData("3");
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay",
                "delay test 20s..." + new Date(), new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setDelayLong(20000L);
                        return message;
                    }
                });
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE, "delay",
                "delay test 10s..." + new Date(), new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setDelayLong(10000L);
                        return message;
                    }
                });
        return "发送成功";
    }
}
